/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller;

import com.dao.TypeUserDAO;
import com.entidad.TypeUser;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Abner Bastarrachea
 */
public class TypeUserController extends HttpServlet {
    TypeUserDAO tuDao = new TypeUserDAO();
    
    @Override
    public void destroy() {
        tuDao.closeEntityManager();
    }

    @Override
    public void init() throws ServletException {
        tuDao = new TypeUserDAO();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String option = request.getParameter("action");
        if(option.equalsIgnoreCase("insertar")){
            response.sendRedirect("TypeUser/addTypeUser.jsp");
        } else if (option.equalsIgnoreCase("guardar")){
            String typeUser = request.getParameter("txtTypeUser");            
            
            TypeUser tu = new TypeUser();
            tu.setTypeUser(typeUser);

            if (tuDao.insertarTypeUser(tu)){
                response.sendRedirect("exito.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }
        } else if (option.equalsIgnoreCase("listado")){
            request.setAttribute("listTypeUser", tuDao.getListTypeUser());
            RequestDispatcher rd = request.getRequestDispatcher("/TypeUser/listTypeUser.jsp");
            rd.forward(request, response);
        } else if (option.equalsIgnoreCase("modificar")){
            Integer id = Integer.parseInt(request.getParameter("codigo"));
            //out.println(id);
            TypeUser tu = tuDao.buscarTypeUser(id);
            if (tu == null)
                response.sendRedirect("found.jsp");
            else{
                request.setAttribute("typeUser", tu);
                RequestDispatcher rd = request.getRequestDispatcher("/TypeUser/editTypeUser.jsp");
                rd.forward(request, response);
            }
        } else if (option.equalsIgnoreCase("eliminar")){
            Integer id = Integer.parseInt(request.getParameter("codigo"));
           
            if (tuDao.eliminarTypeUser(id)){
                response.sendRedirect("exito.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
